package emediashared.model;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class RemoteFile extends UnicastRemoteObject implements IRemoteFile {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String filePath;
	private boolean isDirectory;
	
	
	public RemoteFile(String filePath, boolean isDirectory)  throws RemoteException {
		this.filePath = filePath;
		this.isDirectory = isDirectory;
	}
	
	public String getFilePath() {
		return filePath;
	}
	
	public boolean isDirectory() {
		return isDirectory;
	}
	
	public static File copy(String source, String target, IRemoteProgressMonitor monitor) throws Exception {
		File file2Copy = new File(source);
		File destinationFile = new File(target, file2Copy.getName());
		destinationFile.getParentFile().mkdirs();
		InputStream in = null;
		OutputStream out = null;
		try {
			in = new FileInputStream(file2Copy);
			monitor.beginTask("Copying File: " + file2Copy.getName(), in.available());
			out = new FileOutputStream(destinationFile);
			byte[] buf = new byte[1024];
			int len;
			while ((len = in.read(buf)) > 0) {
				out.write(buf, 0, len);
				monitor.worked(len);
			}
		} finally {
			if (in != null) {
				in.close();
			}
			if (out != null) {
				out.close();
			}
			monitor.done();
		}
		return destinationFile;
	}
	
}
